<template>
  <div class="gallery-container">
    <header class="gallery-header">
      <h1 class="gallery-title">{{ galleryTitle }}</h1>
    </header>

    <div class="image-grid">
      <article
        v-for="image in images"
        :key="image.id"
        class="image-card"
        :class="{ 'selected': image.isSelected }"
        @click="toggleSelect(image)"
      >
        <div class="card-image">
          <img
            :src="image.url"
            :alt="image.name"
            class="responsive-image"
            :style="{ borderColor: image.isSelected ? '#4CAF50' : 'transparent' }"
            @error="handleImageError"
          >
          <div v-if="image.loading" class="loading-overlay">
            <div class="spinner"></div>
          </div>
        </div>

        <div class="card-content">
          <h3 class="image-title">{{ image.name }}</h3>
          <div class="meta-info">
            <p class="upload-time">
              <i class="fas fa-calendar-alt"></i>
              {{ formatDate(image.uploadedAt) }}
            </p>
            <p class="file-size">
              <i class="fas fa-file-archive"></i>
              {{ image.size }}
            </p>
          </div>

          <div class="description-section">
            <div class="raw-html" v-text="image.description"></div>
            <div class="rendered-html" v-html="image.description"></div>
          </div>
        </div>

        <div class="selection-indicator">
          <i class="fas fa-check-circle"></i>
        </div>
      </article>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted } from 'vue';

const galleryTitle = '专业图片素材库';

const images = reactive([
  {
    id: 1,
    name: '自然风光',
    description: '<em class="tag-recommend">🔥 推荐！</em> 美丽的<strong>峡谷景色</strong>',
    url: 'https://img0.baidu.com/it/u=3037722947,3840349077&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750',
    uploadedAt: '2024-03-15',
    size: '2.4MB',
    isSelected: false,
    loading: true
  },
  {
    id: 2,
    name: '城市夜景',
    description: '<span class="tag-hot">🌟 热门</span> 璀璨城市灯光夜景',
    url: 'https://img1.baidu.com/it/u=2053816964,1899026960&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1422',
    uploadedAt: '2024-03-16',
    size: '3.1MB',
    isSelected: false,
    loading: true
  }
]);

// 方法
const toggleSelect = (image) => {
  image.isSelected = !image.isSelected;
};

const formatDate = (dateString) => {
  return new Date(dateString).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
};

const handleImageError = (e) => {
  e.target.src = 'https://source.unsplash.com/random/800x600?error';
};

onMounted(() => {
  images.forEach(img => {
    const image = new Image();
    image.src = img.url;
    image.onload = () => {
      img.loading = false;
    };
  });
});
</script>

<style scoped>
.gallery-container {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 20px;
}

.gallery-header {
  text-align: center;
  margin-bottom: 2rem;
}

.gallery-title {
  font-size: 2.5rem;
  color: #2c3e50;
  text-transform: uppercase;
  letter-spacing: 2px;
  position: relative;
  display: inline-block;
}

.gallery-title::after {
  content: '';
  display: block;
  width: 60%;
  height: 3px;
  background: #4CAF50;
  margin: 10px auto 0;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  padding: 1rem;
}

.image-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  overflow: hidden;
}

.image-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
}

.image-card.selected {
  border: 2px solid #4CAF50;
}

.card-image {
  position: relative;
  height: 250px;
  background: #f5f5f5;
}

.responsive-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px 8px 0 0;
  border: 3px solid transparent;
  transition: border-color 0.3s ease;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #4CAF50;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.card-content {
  padding: 1.5rem;
}

.image-title {
  color: #34495e;
  margin-bottom: 1rem;
  font-size: 1.2rem;
}

.meta-info {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
  color: #7f8c8d;
  font-size: 0.9rem;
}

.meta-info i {
  margin-right: 5px;
}

.description-section {
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

.raw-html {
  background: #f8f9fa;
  padding: 0.8rem;
  margin-bottom: 1rem;
  border-radius: 4px;
  color: #e74c3c;
  font-family: monospace;
}

.rendered-html {
  padding: 0.8rem;
  background: #f8f9fa;
  border-radius: 4px;
}

.tag-recommend {
  color: #27ae60;
}

.tag-hot {
  color: #e67e22;
}

.selection-indicator {
  position: absolute;
  top: 10px;
  right: 10px;
  color: #4CAF50;
  font-size: 1.5rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.image-card.selected .selection-indicator {
  opacity: 1;
}

@media (max-width: 768px) {
  .image-grid {
    grid-template-columns: 1fr;
  }

  .gallery-title {
    font-size: 2rem;
  }
}
</style>
